<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="姓名" size="small">
            <el-input v-model="searchForm.requestName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="所属团队" size="small">
            <el-input v-model="searchForm.teamName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="650px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="120px">
          <el-row>
            <el-col class="dialog-line">
              <el-form-item prop="requestName" label="姓名" size="small">
                <el-input type="text" v-model="teamRow.requestName"></el-input>
              </el-form-item>
              <el-form-item prop="entryTime" label="入职时间" size="small">
                <el-date-picker
                  v-model="teamRow.entryTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 180px"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item prop="endTime" label="试用期结束时间" size="small">
                <el-date-picker
                  v-model="teamRow.endTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 180px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="dialog-line">
              <el-form-item prop="teamName" label="所属团队" size="small" class="item-inline">
                <el-autocomplete
                  v-model="teamRow.teamName"
                  :fetch-suggestions="querySearchTeam"
                  @select="handleSelectTeam"
                  placeholder="请输入内容"
                >
                </el-autocomplete>
              </el-form-item>
              <el-form-item prop="duty" label="职务" size="small">
                <el-input type="text" v-model="teamRow.duty"></el-input>
              </el-form-item>
              <el-form-item label="职级" size="small">
                <el-select v-model="teamRow.rank" placeholder="请选择">
                  <el-option
                    v-for="item in global.rankList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="试用期个人总结" size="small">
              <el-input type="textarea" v-model="teamRow.summarize"></el-input>
            </el-form-item>
            <el-col class="dialog-line">
              <el-form-item label="签字" size="small" class="">
                <el-input type="text" v-model="teamRow.personSign"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="dialog-line">
              <el-form-item label="日期" size="small">
                <el-date-picker
                  v-model="teamRow.personTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 180px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="所属部门意见" size="small">
              <el-select v-model="teamRow.idea" @change="ideaChange" placeholder="请选择">
                <el-option
                  v-for="item in ideaList"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-input type="textarea" v-model="teamRow.departmentIdea"></el-input>
            </el-form-item>
            <el-col class="dialog-line">
              <el-form-item label="部门负责人签字" size="small" class="">
                <el-input type="text" v-model="teamRow.departmentSign"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="dialog-line">
              <el-form-item label="日期" size="small">
                <el-date-picker
                  v-model="teamRow.departmentTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 180px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="人力资源部意见" size="small">
              <el-input type="textarea" v-model="teamRow.hrIdea"></el-input>
            </el-form-item>
            <el-col class="dialog-line">
              <el-form-item label="签字" size="small" class="">
                <el-input type="text" v-model="teamRow.hrSign"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="dialog-line">
              <el-form-item label="日期" size="small">
                <el-date-picker
                  v-model="teamRow.hrTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 180px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="总经理意见" size="small">
              <el-input type="textarea" v-model="teamRow.bossIdea"></el-input>
            </el-form-item>
            <el-col class="dialog-line">
              <el-form-item label="签字" size="small" class="">
                <el-input type="text" v-model="teamRow.bossSign"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="dialog-line">
              <el-form-item label="日期" size="small">
                <el-date-picker
                  v-model="teamRow.bossTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期"
                  style="width: 180px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading">
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.conversionId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    delConversion,
    getList,
    insertConversion,
    selectConversion,
    updateConversion,
    selectTeam
  } from '@/api/positive'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const formatter = function(row, column) {
    if (row[column.property]) {
      let obj = {}
      obj = global[`${column.property}List`].find((item) => {
        return item.value === row[column.property] // 筛选出匹配数据
      })
      return obj.label
    }
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'requestName', label: '姓名', width: '150', fixed: true },
          { prop: 'teamName', label: '所属团队', width: '150' },
          { prop: 'duty', label: '职务', width: '150' },
          { prop: 'rank', label: '职级', width: '150', formatter: formatter },
          { prop: 'entryTime', label: '入职时间', width: '150', component: time },
          { prop: 'endTime', label: '试用期结束时间', width: '150', component: time },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增试用期转正申请表',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        pca: pca,
        rules: {
          requestName: [
            { required: true, message: '请输入姓名', trigger: 'change' }
          ],
          entryTime: [
            { required: true, message: '请选择入职时间', trigger: 'change' }
          ],
          endTime: [
            { required: true, message: '请选择试用期结束时间', trigger: 'change' }
          ],
          teamName: [
            { required: true, message: '请选择所属团队', trigger: 'change' }
          ],
          duty: [
            { required: true, message: '请输入职务', trigger: 'change' }
          ],
          rank: [
            { required: true, message: '请输入职级', trigger: 'change' }
          ]
        },
        visible: false,
        ideaList: [
          { value: '同意按期转正' },
          { value: '建议延长试用期至' },
          { value: '试用期不合格,建议解聘' },
          { value: '其他' }
        ]
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增试用期转正申请表'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
      },
      editTeam(conversionId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改试用期转正申请表'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          conversionId: conversionId
        }
        console.log(params)
        selectConversion(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = {
          requestName: this.teamRow.requestName,
          entryTime: this.teamRow.entryTime,
          endTime: this.teamRow.endTime,
          teamId: this.teamRow.teamId,
          teamName: this.teamRow.teamName,
          duty: this.teamRow.duty,
          rank: this.teamRow.rank,
          summarize: this.teamRow.summarize ? this.teamRow.summarize : '',
          personSign: this.teamRow.personSign ? this.teamRow.personSign : '',
          personTime: this.teamRow.personTime,
          departmentIdea: this.teamRow.departmentIdea ? this.teamRow.departmentIdea : '',
          departmentSign: this.teamRow.departmentSign ? this.teamRow.departmentSign : '',
          departmentTime: this.teamRow.departmentTime,
          hrIdea: this.teamRow.hrIdea ? this.teamRow.hrIdea : '',
          hrSign: this.teamRow.hrSign ? this.teamRow.hrSign : '',
          hrTime: this.teamRow.hrTime,
          bossIdea: this.teamRow.bossIdea ? this.teamRow.bossIdea : '',
          bossSign: this.teamRow.bossSign ? this.teamRow.bossSign : '',
          bossTime: this.teamRow.bossTime
        }
        if (!this.dialogType) {
          params.conversionId = this.teamRow.conversionId
        }
        if (this.teamRow.teamId) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if (this.dialogType) {
                insertConversion(params).then(response => {
                  this.outerVisible = false
                  this.fetchData()
                })
              } else {
                updateConversion(params).then(response => {
                  this.outerVisible = false
                  this.fetchData()
                })
              }
            } else {
              console.log('error submit!!')
              return false
            }
          })
        } else {
          this.$message({
            message: '该团队不存在',
            type: 'warning'
          })
        }
      },
      deleteRow(scope) {
        console.log(this.$refs[`popover-${scope.$index}`])
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          conversionId: scope.row.conversionId,
          isDel: 1
        }
        delConversion(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      addressChange() {
        this.teamRow.provinceId = this.teamRow.addressId[0]
        this.teamRow.cityId = this.teamRow.addressId[1]
        this.teamRow.address = this.pca[86][this.teamRow.addressId[0]] + this.pca[this.teamRow.addressId[0]][this.teamRow.addressId[1]]
      },
      ideaChange() {
        this.teamRow.departmentIdea = this.teamRow.idea
      },
      querySearchTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.teamRow.teamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectTeam(item) {
        console.log(item)
        this.$set(this.teamRow, 'teamId', item.teamId)
        this.$set(this.teamRow, 'teamName', item.teamName)
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
